import { createStore } from 'vuex'

export default createStore({
  state: {
    token: localStorage.getItem("token") || "",
    routers: localStorage.getItem("routers") || "",
    menus: localStorage.getItem("menus") || "",
    permissions: localStorage.getItem("permissions") || "",
  },
  getters: {
    routers(state) {
      if (state.routers) {
        return JSON.parse(state.routers);
      }
    },
    menus(state) {
      if (state.menus) {
        return JSON.parse(state.menus);
      }
    },
    permissions(state) {
      if (state.permissions) {
        return JSON.parse(state.permissions);
      }
    }
  },
  mutations: {
    login(state, { routers, menus, permissions, token}) {
      state.token = token;
      localStorage.setItem('token', token);

      state.routers = JSON.stringify(routers);
      localStorage.setItem('routers', state.routers);

      // 补充处理 menus
      state.menus = JSON.stringify(menus);
      localStorage.setItem('menus', state.menus);

      // 补充处理 permissions
      state.permissions = JSON.stringify(permissions);
      localStorage.setItem('permissions', state.permissions);
    }
  }
})
